package com.company.tree;

import com.company.base.BaseNode;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class TreeNode extends BaseNode {
    public TreeNode left;
    public TreeNode right;

    public TreeNode(Integer value) {
        super(value);
    }

    public static TreeNode buildSearchTree(List<Integer> list){
        TreeNode root=null;
        TreeNode node = null;
        for (Integer integer : list) {
            if (root == null) {
                root = new TreeNode(integer);
                node = root;
                continue;
            }
            node = root;
            TreeNode parentNode = root;
            while (node!=null){
                parentNode = node;
                if (node.val>integer) {
                    node = node.left;
                }else {
                    node = node.right;
                }
            }
            node = new TreeNode(integer);
            if (parentNode.val>integer) {
                parentNode.left = node;
            }else {
                parentNode.right = node;
            }
        }
        return root;
    }

    public static TreeNode getNodeByValue(TreeNode root,int v){
        return getNode(root,v);
    }
    private static TreeNode getNode(TreeNode root,int v){
        if (root.val == v) {
            return root;
        }
        TreeNode nodeByValue=null;
        if (root.left != null) {
            nodeByValue = getNode(root.left,v);
        }
        if (nodeByValue != null) {
            return nodeByValue;
        }
        if (root.right != null) {
            nodeByValue = getNode(root.right,v);
        }
        return nodeByValue;
    }
    public static TreeNode buildSampleTree() {
        TreeNode root = new TreeNode(0);

        root.left = new TreeNode(1);
        root.right = new TreeNode(2);

        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);

        root.right.left = new TreeNode(5);
        root.right.right = new TreeNode(6);


        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("        0        \n");
        stringBuilder.append("    1       2       \n");
        stringBuilder.append("  3   4   5   6     \n");
        System.out.println(stringBuilder.toString());

        return root;
    }

    public static TreeNode buildLineTree() {
        TreeNode root = new TreeNode(0);

        root.left = new TreeNode(1);
        root.right = new TreeNode(2);

        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);

        root.left.left.left = new TreeNode(5);
        root.left.left.right = new TreeNode(6);

        root.left.left.left.left = new TreeNode(7);


        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("          0        \n");
        stringBuilder.append("       1       2       \n");
        stringBuilder.append("     3   4             \n");
        stringBuilder.append("   5   6                \n");
        stringBuilder.append(" 7                \n");
        System.out.println(stringBuilder.toString());

        return root;
    }

    public static void printTree(TreeNode root){
        //程序遍历
        StringBuilder stringBuilder=new StringBuilder();

        Queue<TreeNode> queue=new LinkedBlockingQueue<>();
        Queue<TreeNode> levelQueue=new LinkedBlockingQueue<>();
         queue.add(root);
        while (!queue.isEmpty()&&!levelQueue.isEmpty()){
            //首先如果 queue 不为空 则需要在 levelQueu 里面进行添加 这一层的 queue





        }



    }
}
